Java编程题-生成5个不重复的随机数 发表于 2020-02-29 | 分类于 Java | 字数统计: 126 字 | 阅读时长 ≈ 1 分钟 生成5个不重复的随机数[1-5] 123456789101112131415161718192021222324252627282930313233343536import java.util.Random;public class RandomTest02 { public static void main(String[] args) { Random r = new Random(); int[] a = new int[5]; int index = 0; while (index < 5) { int temp = r.nextInt(6); if (temp != 0 && !contains(a, temp)) { a[index++] = temp; } } for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } // 判断temp元素在a数组中是否包含 private static boolean contains(int[] a, int temp) { for (int i = 0; i < a.length; i++) { if (a[i] == temp) { return true; } } return false; }}